package com.gl.saasservice.sys.controller;

import com.gl.framework.web.response.Result;
import com.gl.saasservice.sys.service.RoleService;
import com.gl.saasservice.sys.vo.RolePermissionRequestVO;
import com.gl.saasservice.sys.vo.RoleVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@Controller
@RequestMapping(path = "/role")
public class RoleController {

    @Autowired
    private RoleService roleService;

    /**
     * 角色列表--APP管理中的角色管理，只展示后管新增的角色(org_id=-1)
     *
     * @param pageNumber
     * @param pageSize
     * @return
     */
    @RequestMapping(value = "/appPages", method = RequestMethod.GET)
    @ResponseBody
    public Result appPages(@RequestHeader String token,
                           @RequestParam(defaultValue = "0") Integer pageNumber,
                           @RequestParam(defaultValue = "10") Integer pageSize,
                           RoleVO roleVO) {
        return roleService.pages(token, pageNumber, pageSize, roleVO, "app");
    }

    /**
     * 角色列表--企业管理的角色管理，展示org_id != -1
     *
     * @param pageNumber
     * @param pageSize
     * @return
     */
    @RequestMapping(value = "/pages", method = RequestMethod.GET)
    @ResponseBody
    public Result pages(@RequestHeader String token,
                        @RequestParam(defaultValue = "0") Integer pageNumber,
                        @RequestParam(defaultValue = "10") Integer pageSize,
                        RoleVO roleVO) {
        return roleService.pages(token, pageNumber, pageSize, roleVO, "web");
    }

    /**
     * 添加角色信息
     *
     * @param roleVO
     * @return
     */
    @RequestMapping(method = RequestMethod.POST)
    @ResponseBody
    public Result add(@RequestHeader String token,
                      @RequestBody RoleVO roleVO) {
        return roleService.save(token, roleVO);
    }

    /**
     * 更新角色信息
     *
     * @param roleVO
     * @return
     */
    @RequestMapping(method = RequestMethod.PUT)
    @ResponseBody
    public Result update(@RequestHeader String token,
                         @RequestBody RoleVO roleVO) {
        return roleService.save(token, roleVO);
    }

    /**
     * 删除角色信息
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "{id}", method = RequestMethod.DELETE)
    @ResponseBody
    public Result delete(@RequestHeader String token,
                         @PathVariable Long id) {
        return roleService.delete(token, id);
    }

    /**
     * 获取角色授权列表
     *
     * @param roleId
     * @return
     */

    @RequestMapping(value = "/permissionList/{roleId}", method = RequestMethod.GET)
    @ResponseBody
    public Result permissionList(@RequestHeader String token,
                                 @PathVariable Long roleId) {
        return roleService.permissionList(token, roleId);
    }

    /**
     * 授权角色权限
     *
     * @param roleId
     * @param vo
     * @author
     */
    @PostMapping(value = "/permissionSave/{roleId}")
    @ResponseBody
    public Result permissionSave(@RequestHeader String token,
                                 @PathVariable Long roleId,
                                 @RequestBody RolePermissionRequestVO vo) {
        return roleService.saveRolePermission(token, roleId, vo.getPermissionIds());
    }
}
